package com.slife.serial.utils;

import cn.hutool.core.util.HexUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class ElecticUtils {

    public static String parseMethod1(String hex){

        if (hex.length()>6){
            Integer length = HexUtil.hexToInt(hex.substring(4, 6))*2;
            String substring = hex.substring(6, 6+length);
            String[] xlist = splitStringByLength(substring, 4);
            Collections.reverse(Arrays.asList(xlist));
            String join = StringUtils.join(xlist, "");
            byte[] xbytes = HexUtils.hexToByte(join);
            BigDecimal bigDecimal = IEEE754Utils.bytesToSingle(xbytes);
            return bigDecimal.setScale(2, RoundingMode.HALF_UP).toString();
        }else {
            return "";
        }

    }

    public static String parseToValue(String hex){

        if (hex.length() == 18){
            return parseMethod1111(hex);

        }
        else if (hex.length()==26){
            return parseMethod222222(hex);
        }else {
            return "暂未支持";
        }
    }

    public static String parseMethod1111(String hex){

        if (hex.length()>6){
            Integer length =8;
            String substring = hex.substring(6, 6+length);
            String[] xlist = splitStringByLength(substring, 4);
            Collections.reverse(Arrays.asList(xlist));
            String join = StringUtils.join(xlist, "");
            byte[] xbytes = HexUtils.hexToByte(join);
            BigDecimal bigDecimal = IEEE754Utils.bytesToSingle(xbytes);
            return bigDecimal.setScale(2, RoundingMode.HALF_UP).toString();
        }else {
            return "";
        }

    }

    public static String[] splitStringByLength(String input, int length) {
        int numOfSubstrings = (int) Math.ceil((double) input.length() / length);
        String[] substrings = new String[numOfSubstrings];
        for (int i = 0; i < numOfSubstrings; i++) {
            int startIndex = i * length;
            int endIndex = Math.min(startIndex + length, input.length());
            substrings[i] = input.substring(startIndex, endIndex);
        }

        return substrings;
    }

    public static String parseMethod222222(String hex) {

        if (hex.length()>6){
            Integer length = HexUtil.hexToInt(hex.substring(4, 6))*2;
            String substring = hex.substring(6, 6+length);
            String[] xlist = splitStringByLength(substring, 8);
            ArrayList<Integer> rList = new ArrayList<>();
            for (String key: xlist) {
                String[] list1 = splitStringByLength(key, 4);
                Collections.reverse(Arrays.asList(list1));
                String hex4 = StringUtils.join(list1, "");
                int i1 = HexUtil.hexToInt(hex4);
                rList.add(i1);
            }
            return StringUtils.join(rList,".");

        }else {
            return "";
        }

    }


    public static void main(String[] args) {
        String s1 = "080404226E413F7961";

        String s = parseToValue(s1);
        System.out.println(s);

    }
}
